'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 用户相关
 * @author sundbcn@gmail.com (sun debing)
 */

var _ = require('lodash');
var Code = require('../../../consts/code');
var moment = require('moment');
var roleModel = require('../../../models/roleModel');
var userModel = require('../../../models/userModel');
var battleModel = require('../../../models/battleModel');
var mailModel = require('../../../models/mailModel');
var async = require('async');

module.exports = function (app) {
    return new Handler(app);
};

var Handler = function (app) {
    this.app = app;
};

/**
 * 等级列表
 * @param  {[type]}   msg     [description]
 * @param  {[type]}   session [description]
 * @param  {Function} next    [description]
 * @return {[type]}           [description]
 */
Handler.prototype.getLevelList = function (msg, session, next) {
    var begin_lv = +msg.query.begin_lv;
    var end_lv = +msg.query.end_lv;
    roleModel.getLevelList(begin_lv, end_lv, function (err, result) {
        if (!!err) {
            console.error(err);
            return next(null, {
                code: Code.FAIL
            });
        }
        var level_list = [];
        _.each(result, function (doc) {
            level_list.push({
                lv: doc._id,
                count: doc.count
            });
        });
        return next(null, {
            code: Code.OK,
            result: {
                level_list: level_list
            }
        });
    });
};

/**
 * 获取用户信息
 * @param  {[type]}   msg     [description]
 * @param  {[type]}   session [description]
 * @param  {Function} next    [description]
 * @return {[type]}           [description]
 */
Handler.prototype.getInfoByRoleId = function (msg, session, next) {
    var role_id = msg.query.role_id;
    var role_info = {};
    async.series(
        [
            function (cb) {
                roleModel.getRoleInfoByRoleId(role_id, function (err, result) {
                    if (!!err || !result) {
                        return next(null, {
                            code: Code.FAIL
                        });
                    }
                    role_info = result;
                    cb();
                });
            },
            function (cb) {
                battleModel.getByRoleId(role_id, function (err, battle_model) {
                    if (!!err || !battle_model) {
                        return next(null, {
                            code: Code.FAIL
                        });
                    }
                    role_info.noble = battle_model.noble;
                    cb();
                });
            },
            function (cb) {
                userModel.getByUid(role_info.uid, function (err, user_model) {
                    if (!!err || !user_model) {
                        return next(null, {
                            code: Code.FAIL
                        });
                    }
                    role_info.create_time = moment(user_model.create_time).unix();
                    cb();
                });
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK,
                result: role_info
            });
        }
    );
};

/**
 * 根据用户名获取用户信息
 * @param  {[type]}   msg     [description]
 * @param  {[type]}   session [description]
 * @param  {Function} next    [description]
 * @return {[type]}           [description]
 */
Handler.prototype.getInfoByName = function (msg, session, next) {
    var name = msg.query.name;
    roleModel.getRoleInfoByName(name, function (err, role_info) {
        if (!!err || !role_info) {
            return next(null, {
                code: Code.FAIL
            });
        }

        return next(null, {
            code: Code.OK,
            result: {
                role_info: role_info
            }
        });
    });
};

/**
 * 根据用户uid封用户
 * @param  {[type]}   msg     [description]
 * @param  {[type]}   session [description]
 * @param  {Function} next    [description]
 * @return {[type]}           [description]
 */
Handler.prototype.setForbid = function (msg, session, next) {
    var self = this;
    var uid = msg.query.uid;
    var start_time = moment.unix(msg.query.start_time);
    var end_time = moment.unix(msg.query.end_time);
    var reason = msg.query.reason;
    async.waterfall(
        [
            function (cb) {
                userModel.getByUid(uid, cb);
            },
            function (model, cb) {
                model.setForbid(start_time, end_time, reason);
                model.save(function (err, data) {
                    cb(err);
                });
            },
            function (cb) {
                roleModel.getRoleList(uid, cb);
            },
            function (list, cb) {
                var role_id_list = [];
                _.forEach(list, function (item) {
                    role_id_list.push(item.role_id);
                });

                _kickUserForList(self.app, session, msg, role_id_list, cb);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK
            });
        }
    );
};

var _kickUserForList = function (app, session, msg, role_id_list, callback) {
    if (role_id_list.length < 1) {
        return callback(null);
    }

    async.eachSeries(role_id_list, function (role_id, cb) {
        msg.query = {
            role_id: role_id
        };
        app.rpc.connector.entryRemote.kickUser(session, msg, cb);
    }, function (err) {
        callback(err);
    });
};

/**
 * 根据用户uid解封用户
 * @param  {[type]}   msg     [description]
 * @param  {[type]}   session [description]
 * @param  {Function} next    [description]
 * @return {[type]}           [description]
 */
Handler.prototype.unForbid = function (msg, session, next) {
    var uid = msg.query.uid;
    async.waterfall(
        [
            function (cb) {
                userModel.getByUid(uid, cb);
            },
            function (model, cb) {
                model.unForbid();
                model.save();
                cb();
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK
            });
        }
    );
};

/**
 * 获取封禁用户信息
 * @param  {[type]}   msg     [description]
 * @param  {[type]}   session [description]
 * @param  {Function} next    [description]
 * @return {[type]}           [description]
 */
Handler.prototype.banUserInfo = function (msg, session, next) {
    var ban_list;
    async.waterfall(
        [
            function (cb) {
                userModel.getForbidUser(cb);
            },
            function (result, cb) {
                _getRoleByUid(result, function (err, role_list) {
                    ban_list = role_list;
                    cb();
                });
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK,
                result: ban_list
            });
        }
    );
};

/**
 * 获取用户邮件信息
 * @param  {[type]}   msg     [description]
 * @param  {[type]}   session [description]
 * @param  {Function} next    [description]
 * @return {[type]}           [description]
 */
Handler.prototype.getUserMail = function (msg, session, next) {
    var role_id = msg.query.role_id;
    var mail_list = [];
    async.waterfall(
        [
            function (cb) {
                mailModel.getByRoleId(role_id, cb);
            },
            function (model, cb) {
                _.each(model.getMailList(), function (mail_item) {
                    mail_list.push({
                        title: mail_item.title,
                        content: mail_item.content,
                        attach_list: mail_item.attach_list,
                        create_time: mail_item.create_time
                    });
                });
                cb();
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK,
                result: {
                    mail_list: mail_list
                }
            });
        }
    );
};

/**
 * 根据uid获得用户角色表
 * @param  {[type]}   user_list [description]
 * @param  {Function} cb        [description]
 * @return {[type]}             [description]
 */
var _getRoleByUid = function (user_list, cb) {
    var ban_list = [];
    async.each(user_list, function (user_item, cb) {
        roleModel.getRoleList(user_item.uid, function (err, role_list) {
            _.each(role_list, function (role_item) {
                ban_list.push({
                    uid: role_item.uid,
                    role_id: role_item.role_id,
                    name: role_item.name,
                    start_time: moment(user_item.start_time).unix(),
                    end_time: moment(user_item.end_time).unix(),
                    reason: user_item.reason,
                    forbid: user_item.forbid
                });
            });
            cb();
        });
    }, function (err) {
        if (!!err) {
            return cb();
        }
        cb(null, ban_list);
    });
};
